package cn.iocoder.yudao.module.design.controller.admin.userlabel;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.userlabel.vo.*;
import cn.iocoder.yudao.module.design.convert.userlabel.UserLabelConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.userlabel.UserLabelDO;
import cn.iocoder.yudao.module.design.service.userlabel.UserLabelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 用户可提供服务标签")
@RestController
@RequestMapping("/design/user-label")
@Validated
public class UserLabelController {

    @Resource
    private UserLabelService userLabelService;

    @PostMapping("/create")
    @Operation(summary ="创建用户可提供服务标签")
    @PreAuthorize("@ss.hasPermission('design:user-label:create')")
    public CommonResult<Long> createUserLabel(@Valid @RequestBody UserLabelCreateReqVO createReqVO) {
        return success(userLabelService.createUserLabel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新用户可提供服务标签")
    @PreAuthorize("@ss.hasPermission('design:user-label:update')")
    public CommonResult<Boolean> updateUserLabel(@Valid @RequestBody UserLabelUpdateReqVO updateReqVO) {
        userLabelService.updateUserLabel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除用户可提供服务标签")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('design:user-label:delete')")
    public CommonResult<Boolean> deleteUserLabel(@RequestParam("id") Long id) {
        userLabelService.deleteUserLabel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得用户可提供服务标签")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:user-label:query')")
    public CommonResult<UserLabelRespVO> getUserLabel(@RequestParam("id") Long id) {
        UserLabelDO userLabel = userLabelService.getUserLabel(id);
        return success(UserLabelConvert.INSTANCE.convert(userLabel));
    }

    @GetMapping("/list")
    @Operation(summary ="获得用户可提供服务标签列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('design:user-label:query')")
    public CommonResult<List<UserLabelRespVO>> getUserLabelList(@RequestParam("ids") Collection<Long> ids) {
        List<UserLabelDO> list = userLabelService.getUserLabelList(ids);
        return success(UserLabelConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得用户可提供服务标签分页")
    @PreAuthorize("@ss.hasPermission('design:user-label:query')")
    public CommonResult<PageResult<UserLabelRespVO>> getUserLabelPage(@Valid UserLabelPageReqVO pageVO) {
        PageResult<UserLabelDO> pageResult = userLabelService.getUserLabelPage(pageVO);
        return success(UserLabelConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出用户可提供服务标签 Excel")
    @PreAuthorize("@ss.hasPermission('design:user-label:export')")
   
    public void exportUserLabelExcel(@Valid UserLabelExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserLabelDO> list = userLabelService.getUserLabelList(exportReqVO);
        // 导出 Excel
        List<UserLabelExcelVO> datas = UserLabelConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户可提供服务标签.xls", "数据", UserLabelExcelVO.class, datas);
    }


    @GetMapping("/getUserLabelByUserId")
    @Operation(summary ="获得用户可提供服务标签")
    @Parameter(name = "id", description = "供应商编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:user-label:query')")
    public CommonResult<List<UserLabelVO>> getUserLabelByUserId(@RequestParam("id") Long userId) {
        List<UserLabelDO> userLabelList = userLabelService.getUserLabelByUserId(userId);
        return success(UserLabelConvert.INSTANCE.convertVoList(userLabelList));
    }
}
